package de.jmda.gen.java.naming;

import java.util.Map;

import javax.lang.model.element.TypeElement;

public class ImportManagerProvider
{
	private final static ImportManagerProvider INSTANCE =
			new ImportManagerProvider();

	private ImportManager importManager;

	private ImportManagerProvider()
	{
		super();
	}

	public static ImportManager newImportManager(
			String generationTargetPackageName,
			String generationTargetSimpleName,
			Map<TypeElement, String> generationTypeToTargetQualifiedTypeName)
	{
		ImportManager result = INSTANCE.importManager;
		
		INSTANCE.importManager =
				new ImportManager(
						generationTargetPackageName,
						generationTargetSimpleName,
						generationTypeToTargetQualifiedTypeName);
		
		return result;
	}

	/**
	 * Creates new import manager for <code>packagename</code> and stores it in
	 * {@link #importManager}.
	 *
	 * @param packagename
	 * @param generationTargetSimpleName
	 * @return import manager that was previously stored in {@link #importManager}
	 */
	public static ImportManager newImportManager(
			String generationTargetPackageName, String generationTargetSimpleName)
	{
		return
				newImportManager(
						generationTargetPackageName, generationTargetSimpleName, null);
	}

	public static ImportManager newImportManager()
	{
		return newImportManager(null, null);
	}

	public static void restoreImportManager(ImportManager importManager)
	{
		INSTANCE.importManager = importManager;
	}

	public static ImportManager getImportManager()
	{
		if (INSTANCE.importManager == null)
		{
			INSTANCE.importManager = new ImportManager();
		}

		return INSTANCE.importManager;
	}
}